// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   counter.java

package include.nseer_cookie;

import include.nseer_db.nseer_db;
import include.nseer_db.nseer_db_backup;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;

public class counter
{

	private String kind;
	private String kind_name;
	private int kind_value;
	private String sql;
	private ResultSet rs;
	private int filenum;
	private ServletContext dbApplication;

	public counter(ServletContext dbApplication)
	{
		kind = "";
		kind_name = "";
		sql = "";
		rs = null;
		this.dbApplication = dbApplication;
	}

	public int read(String unit_db_name, String kind)
	{
		this.kind = kind;
		nseer_db erp_db = new nseer_db(unit_db_name);
		kind = kind.toLowerCase();
		try
		{
			sql = (new StringBuilder("select * from security_counter where kind='")).append(kind).append("'").toString();
			rs = erp_db.executeQuery(sql);
			if (rs.next())
				filenum = Integer.parseInt(rs.getString("count_value"));
			erp_db.close();
		}
		catch (Exception exception) { }
		return filenum;
	}

	public int readTime(String dbase, String mod)
	{
		String time = "";
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		time = formatter.format(now);
		nseer_db erp_db = new nseer_db(dbase);
		mod = mod.substring(mod.lastIndexOf("/") + 1);
		StringTokenizer tokenTOO = new StringTokenizer(mod, "_");
		String sqla = "";
		String main_kind1 = "";
		String first_kind1 = "";
		if (tokenTOO.hasMoreTokens())
		{
			main_kind1 = tokenTOO.nextToken();
			first_kind1 = tokenTOO.nextToken();
		}
		String name_kind = (new StringBuilder(String.valueOf(main_kind1))).append(first_kind1).append("count").toString();
		String kind = (new StringBuilder(String.valueOf(name_kind))).append("_").append(time).toString();
		try
		{
			sql = (new StringBuilder("select * from security_counter where kind_name='")).append(name_kind).append("'").toString();
			rs = erp_db.executeQuery(sql);
			if (rs.next())
			{
				if (kind.equals(rs.getString("kind")))
				{
					filenum = Integer.parseInt(rs.getString("count_value"));
				} else
				{
					sqla = (new StringBuilder("update security_counter set kind='")).append(kind).append("', count_value='100001' where kind_name='").append(name_kind).append("'").toString();
					erp_db.executeUpdate(sqla);
					filenum = 0x186a1;
				}
			} else
			{
				sqla = (new StringBuilder("insert into security_counter(kind,count_value,kind_name) values ('")).append(kind).append("','100001','").append(name_kind).append("')").toString();
				erp_db.executeUpdate(sqla);
				filenum = 0x186a1;
			}
			erp_db.close();
		}
		catch (Exception exception) { }
		return filenum;
	}

	public void writeTime(String dbase, String mod)
	{
		String time = "";
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		time = formatter.format(now);
		nseer_db erp_db = new nseer_db(dbase);
		mod = mod.substring(mod.lastIndexOf("/") + 1);
		StringTokenizer tokenTOO = new StringTokenizer(mod, "_");
		String sqla = "";
		String main_kind1 = "";
		String first_kind1 = "";
		if (tokenTOO.hasMoreTokens())
		{
			main_kind1 = tokenTOO.nextToken();
			first_kind1 = tokenTOO.nextToken();
		}
		String name_kind = (new StringBuilder(String.valueOf(main_kind1))).append(first_kind1).append("count").toString();
		String kind = (new StringBuilder(String.valueOf(name_kind))).append("_").append(time).toString();
		try
		{
			sql = (new StringBuilder("select * from security_counter where kind_name='")).append(name_kind).append("'").toString();
			rs = erp_db.executeQuery(sql);
			if (rs.next())
			{
				filenum = Integer.parseInt(rs.getString("count_value"));
				filenum++;
				sqla = (new StringBuilder("update security_counter set count_value='")).append(filenum).append("' where kind_name='").append(name_kind).append("'").toString();
				erp_db.executeUpdate(sqla);
			}
			erp_db.close();
		}
		catch (Exception exception) { }
	}

	public int readTime1(String dbase, String mod)
	{
		String time = "";
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		time = formatter.format(now);
		nseer_db erp_db = new nseer_db(dbase);
		mod = mod.substring(mod.lastIndexOf("/") + 1);
		StringTokenizer tokenTOO = new StringTokenizer(mod, "_");
		String sqla = "";
		String main_kind1 = "";
		String first_kind1 = "";
		String second_kind1 = "";
		if (tokenTOO.hasMoreTokens())
		{
			main_kind1 = tokenTOO.nextToken();
			first_kind1 = tokenTOO.nextToken();
			second_kind1 = tokenTOO.nextToken();
		}
		String name_kind = (new StringBuilder(String.valueOf(main_kind1))).append(first_kind1).append(second_kind1).append("count").toString();
		String kind = (new StringBuilder(String.valueOf(name_kind))).append("_").append(time).toString();
		try
		{
			sql = (new StringBuilder("select * from security_counter where kind_name='")).append(name_kind).append("'").toString();
			rs = erp_db.executeQuery(sql);
			if (rs.next())
			{
				if (kind.equals(rs.getString("kind")))
				{
					filenum = Integer.parseInt(rs.getString("count_value"));
				} else
				{
					sqla = (new StringBuilder("update security_counter set kind='")).append(kind).append("', count_value='100001' where kind_name='").append(name_kind).append("'").toString();
					erp_db.executeUpdate(sqla);
					filenum = 0x186a1;
				}
			} else
			{
				sqla = (new StringBuilder("insert into security_counter(kind,count_value,kind_name) values ('")).append(kind).append("','100001','").append(name_kind).append("')").toString();
				erp_db.executeUpdate(sqla);
				filenum = 0x186a1;
			}
			erp_db.close();
		}
		catch (Exception exception) { }
		return filenum;
	}

	public void writeTime1(String dbase, String mod)
	{
		String time = "";
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		time = formatter.format(now);
		nseer_db erp_db = new nseer_db(dbase);
		mod = mod.substring(mod.lastIndexOf("/") + 1);
		StringTokenizer tokenTOO = new StringTokenizer(mod, "_");
		String sqla = "";
		String main_kind1 = "";
		String first_kind1 = "";
		String second_kind1 = "";
		if (tokenTOO.hasMoreTokens())
		{
			main_kind1 = tokenTOO.nextToken();
			first_kind1 = tokenTOO.nextToken();
			second_kind1 = tokenTOO.nextToken();
		}
		String name_kind = (new StringBuilder(String.valueOf(main_kind1))).append(first_kind1).append(second_kind1).append("count").toString();
		String kind = (new StringBuilder(String.valueOf(name_kind))).append("_").append(time).toString();
		try
		{
			sql = (new StringBuilder("select * from security_counter where kind_name='")).append(name_kind).append("'").toString();
			rs = erp_db.executeQuery(sql);
			if (rs.next())
			{
				filenum = Integer.parseInt(rs.getString("count_value"));
				filenum++;
				sqla = (new StringBuilder("update security_counter set count_value='")).append(filenum).append("' where kind_name='").append(name_kind).append("'").toString();
				erp_db.executeUpdate(sqla);
			}
			erp_db.close();
		}
		catch (Exception exception) { }
	}

	public boolean write(String unit_db_name, String kind_name, int kind_value)
	{
label0:
		{
			this.kind_name = kind_name;
			this.kind_value = kind_value;
			nseer_db_backup erp_db1 = new nseer_db_backup(dbApplication);
			kind_name = kind_name.toLowerCase();
			try
			{
				kind_value++;
				if (erp_db1.conn(unit_db_name))
				{
					sql = (new StringBuilder("update security_counter set count_value='")).append(kind_value).append("' where kind='").append(kind_name).append("'").toString();
					erp_db1.executeUpdate(sql);
					erp_db1.close();
					break label0;
				}
			}
			catch (Exception ex)
			{
				return false;
			}
			return false;
		}
		return true;
	}
}
